﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;


namespace color2gray
{
    class fileJpeg
    {
        private Image myImage;
        public Image getImage()
        {
            return myImage;
        }
        public void setImage(Image theImage)
        {
            myImage = theImage;
        }
        public Image openImage(OpenFileDialog openFileDialog1)
        {
            openFileDialog1.Title = "Choose the image";
            openFileDialog1.Filter = "JPEG Images|*.jpg";
            openFileDialog1.ShowDialog();
            string Chosen_File = "";
            Chosen_File = openFileDialog1.FileName;

            if (Chosen_File == "")
            {
                return null;
            }

            try
            {
                myImage = Image.FromFile(Chosen_File);
            }
            catch
            {
                return null;
            }
            
            return myImage;
        }
        public void saveImage(SaveFileDialog saveFileDialog1)
        {
            if (myImage == null)
            {
                return;
            }

            string strFileName = "";
            Image image = myImage;

            saveFileDialog1.Title = "Choose the directory";
            saveFileDialog1.InitialDirectory = "C:";
            saveFileDialog1.FileName = Path.GetFileNameWithoutExtension(strFileName);
            saveFileDialog1.AddExtension = true;
            saveFileDialog1.Filter = "Windows Bitmap (*.bmp)|*.bmp|" +
                           "Graphics Interchange Format (*.gif)|*.gif|" +
                           "JPEG File Interchange Format (*.jpg)|" +
                              "*.jpg;*.jpeg;*.jfif|" +
                           "Portable Network Graphics (*.png)|*.png|" +
                           "Tagged Imaged File Format (*.tif)|*.tif;*.tiff";

            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    image.Save(saveFileDialog1.FileName);
                }
                catch
                {
                    return;
                }
                strFileName = saveFileDialog1.FileName;

            }
        }

    }
}
